<?php

namespace App\Containers\AppSection\Moneybox\Actions;

use App\Containers\AppSection\Moneybox\Models\Moneybox;
use App\Containers\AppSection\Moneybox\Tasks\FindMoneyboxByUUIDTask;
use App\Containers\AppSection\Moneybox\Tasks\UpdateMoneyboxTask;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Exceptions\UpdateResourceFailedException;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Requests\Request;
use Illuminate\Support\Facades\Storage;

class StatusNotificationAction extends Action
{
    /**
     * @throws UpdateResourceFailedException
     * @throws NotFoundException
     */
    public function run(Request $request)
    {
        $moneybox = app(FindMoneyboxByUUIDTask::class)->run($request->getInputByKey("uuid"));
        if ($request->getInputByKey("is_success")) {
            app(UpdateMoneyboxTask::class)->run($moneybox->id, [
                "response" => [],
                "response_time" => now(),
                "status" => 1,
            ]);
        } else {
            app(UpdateMoneyboxTask::class)->run($moneybox->id, [
                "response" => $request->all(),
                "response_time" => now(),
                "status" => 0,
            ]);
        }

        if ($moneybox->method === config('appSection-moneybox.methods.beneficiary_registry')) {
            Storage::disk('public')->deleteDirectory('beneficiary_registry/' . $moneybox->uuid);
        }
    }
}
